<?php

class basketPageComponents extends myComponents{

	public function executeRender(){
		
		$this->component = 'basketPage';
		$this->action = 'userBasketList';

		switch($this->getRequestParameter('step',1)){
			case 1:
				break;
			case 2:
				$this->action = 'userData';
				break;
			case 3:
				$this->action = 'userAddresses';
				break;
			case 4:
				$this->action = 'userOrderOverview';
				break;
			case 5:
				$this->action = 'userOrderCompleted';
				break;
			default:
				throw new Exception('no such step');
				break;
		}
	}

	
	public function executeUserOrderCompleted(){
		$this->node = $this->getRoute()->getObject();
		$this->order = Doctrine::getTable('ProductOrder')->find($this->getRequestParameter('orderID'));
		$this->basket = myBasket::getInstance(myBasket::CHECKED_OUT, $this->order);
		$this->products = $this->basket->isEmpty() ? array() : $this->basket->prepareDataForView();
	}

	
	public function executeUserData(){
		$this->node = $this->getRoute()->getObject();
		$this->form = new SignInForm();
	}

	public function executeUserAddresses(){
		$this->node = $this->getRoute()->getObject();

		$this->shippingAddresses = $this->getShippingAddresses();
		$obj = new PublicUserAddresses();
		$obj->fromArray(
				array_merge(
					myBasket::getInstance(myBasket::SESSION_BASED)->getShippingAddress(),
					array('type' => 'shipping')
				)
		);
		$this->shippingAddressForm = new PublicUserAddressForm($obj);
		
		
		$this->billingAddresses = $this->getBillingAddresses();
		$obj = new PublicUserAddresses();
		$obj->fromArray(
				array_merge(
						myBasket::getInstance(myBasket::SESSION_BASED)->getBillingAddress(),
						array('type' => 'billing')
				)
		);
		$this->billingAddressForm = new PublicUserAddressForm($obj);

	}

	public function executeUserOrderOverview(){
		$this->node = $this->getRoute()->getObject();
		$this->basket = myBasket::getInstance(myBasket::SESSION_BASED);
		$this->products = $this->basket->isEmpty() ? array() : $this->basket->prepareDataForView();
		$this->user = $this->getUser()->getObject();

		$this->termsAndConditionsNode = Doctrine::getTable('Structure')->findOneByParameterAndLang('terms_and_conditions', $this->getRoute()->getObject()->getLang());

		$this->totalPayable = $this->basket->getTotalPayable();
	}

	public function executeUserBasketList(){
		$this->basket = myBasket::getInstance(myBasket::SESSION_BASED);

		$this->node = $this->getRoute()->getObject();

		$this->products = $this->basket->isEmpty() ? array() : $this->basket->prepareDataForView();
		$this->subtotalPrice = $this->basket->getTotalPrice();
		$this->totalPayable = $this->basket->getTotalPayable();

		$this->shippingZones = $this->getShippingZones();

		$this->activeShippingZone = $this->getActiveShippingZone();
	}


	public function getShippingAddresses(){
            if($this->getUser()->isAuthenticated()) {
		return Doctrine::getTable('PublicUserShippingAddress')->findByPublicUserId($this->getUser()->getObject()->getId());
            } else {
              return false;
            }
        }

	public function getBillingAddresses(){
          if($this->getUser()->isAuthenticated()) {
		return Doctrine::getTable('PublicUserBillingAddress')->findByPublicUserId($this->getUser()->getObject()->getId());
          } else {
                return false;
          }

        }
	
	private function getActiveShippingZone(){
		return myBasket::getInstance(myBasket::SESSION_BASED)->getShippingZone();
	}

	private function getShippingZones(){
		$q = Doctrine::getTable('ShippingZone')->createQuery()
			->from('ShippingZone s')
			->innerJoin("s.Translation t WITH t.lang = ? AND t.name != ?",array($this->getUser()->getCulture(), ''));

		return $q->execute();
	}
}